<?php

/**
 * OrganizationalUnit Actions
 *
 * @package    wildlive
 * @subpackage organizational_unit
 * @author     Maximilian Lenkeit <dev@lenki.com>
 */
class organizational_unitActions extends WildliveActions
{

  public function executePortal (sfWebRequest $request)
  {
    if ($this->getRoute() instanceof sfObjectRoute && $this->getRoute()
      ->getObject() instanceof OrganizationalUnit)
    {
      $organizational_unit = $this->getRoute()
        ->getObject();
    }
    else
    {
      $user = $this->getUser();
      $organizational_unit = $user->getOrganizationalUnit();
    }
    $this->organizational_unit = $organizational_unit;
    
    $this->related_organizational_units = new Doctrine_Collection('OrganizationalUnit'); //$this->organizational_unit->getRadiusOneRelatives();
    $this->related_organizational_units->add($this->getUser()
      ->getOrganizationalUnit());
    $descendants = $this->getUser()
      ->getOrganizationalUnit()
      ->getDescendants();
    if($descendants instanceof Doctrine_Collection) {
      $this->related_organizational_units->merge($descendants);
    }
    
    $this->members = $organizational_unit->retrieveMembers();
  }

  public function executeIndex (sfWebRequest $request)
  {
    $roots = OrganisationalEntityTable::getInstance()->getRoots();
    $treeObject = Doctrine_Core::getTable('OrganisationalEntity')->getTree();
    foreach ($roots as $root)
    {
      $col = $treeObject->fetchTree(array(
        'root_id' => $root->getId()));
      if ($this->organisational_entities instanceof Doctrine_Collection)
      {
        $this->organisational_entities->merge($col);
      }
      else
      {
        $this->organisational_entities = $col;
      
   #echo get_class($this->organisational_entities);
      }
    }
    $this->max_level = OrganisationalEntityTable::getInstance()->getMaxLevel();
  }

  public function executeShow (sfWebRequest $request)
  {
    $this->organisational_entity = Doctrine_Core::getTable('OrganisationalEntity')->find(array(
      $request->getParameter('id')));
    $this->forward404Unless($this->organisational_entity);
  }

  public function executeNew (sfWebRequest $request)
  {
    $this->parent_organizational_unit = $this->getRoute()
      ->getObject();
    $this->form = new OrganizationalUnitForm(new OrganizationalUnit(), 
        array(
          OrganizationalUnitForm::OPTION_PARENT_WIDGET => OrganizationalUnitForm::PARENT_WIDGET_HIDDEN, 
            OrganizationalUnitForm::OPTION_PARENT => $this->parent_organizational_unit));
  }

  public function executeCreate (sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));
    
    //$this->form = new OrganizationalUnitForm();
    $this->form = new OrganizationalUnitForm(new OrganizationalUnit(), 
        array(
          OrganizationalUnitForm::OPTION_PARENT_WIDGET => OrganizationalUnitForm::PARENT_WIDGET_HIDDEN));
    
    $this->processForm($request, $this->form);
    
    $this->setTemplate('new');
  }

  public function executeEdit (sfWebRequest $request)
  {
    //    $this->forward404Unless(
    //    $organisational_entity = Doctrine_Core::getTable('OrganisationalEntity')->find(array($request->getParameter('id'))), 
    //    sprintf('Object organisational_entity does not exist (%s).', $request->getParameter('id')));
    $organizational_unit = $this->getRoute()
      ->getObject();
    $this->parent_organizational_unit = $organizational_unit->getNode()
      ->getParent();
    $this->form = new OrganizationalUnitForm($organizational_unit, 
        array(
          OrganizationalUnitForm::OPTION_PARENT_WIDGET => OrganizationalUnitForm::PARENT_WIDGET_HIDDEN, 
            OrganizationalUnitForm::OPTION_PARENT => $this->parent_organizational_unit, OrganizationalUnitForm::OPTION_ENTITY_ID_READONLY => true));
  }

  public function executeUpdate (sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($organizational_unit = OrganizationalUnitTable::getInstance()->find(array(
      $request->getParameter('id'))), sprintf('Untergliederung existiert nicht (%s).', $request->getParameter('id')));
    
    $this->form = new OrganizationalUnitForm($organizational_unit, 
        array(
          OrganizationalUnitForm::OPTION_PARENT_WIDGET => OrganizationalUnitForm::PARENT_WIDGET_HIDDEN, 
            OrganizationalUnitForm::OPTION_ENTITY_ID_READONLY => true));
    
    $this->processForm($request, $this->form);
    
    $this->setTemplate('edit');
  }

  public function executeDelete (sfWebRequest $request)
  {
    $request->checkCSRFProtection();
    
    $this->forward404Unless(
        $organisational_entity = Doctrine_Core::getTable('OrganisationalEntity')->find(array(
          $request->getParameter('id'))), sprintf('Object organisational_entity does not exist (%s).', $request->getParameter('id')));
    $organisational_entity->delete();
    
    $this->redirect('organisational_entity/index');
  }

  protected function processForm (sfWebRequest $request, OrganizationalUnitForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    $this->getLogger()
      ->log('IsBound: ' . var_export($form->isBound(), true));
    $this->getLogger()
      ->log('FormValues: ' . print_r($form->getValues(), true));
    if ($form->isValid())
    {
      $is_new = $form->getObject()
        ->isNew();
      $organizational_unit = $form->save();
      if ($is_new)
      {
        $parent_id = $form->getValue('parent_id');
        $parent_organizational_unit = OrganizationalUnitTable::getInstance()->findOneById($parent_id);
        $this->parent_organizational_unit = $parent_organizational_unit;
        $this->forward404Unless($parent_organizational_unit, sprintf('Untergliederung konnte nicht gefunden werden (%s)', $parent_id));
        $organizational_unit->getNode()
          ->insertAsLastChildOf($parent_organizational_unit);
        $this->getUser()
          ->setFlash(UserFlash::SUCCESS, UserFlash::CREATION_SAVED_SUCCESSFULLY);
      }
      else
      {
        $this->getUser()
          ->setFlash(UserFlash::SUCCESS, UserFlash::CHANGES_SAVED_SUCCESSFULLY);
      }
      //$this->redirect($this->generateUrl('organizational_unit_portal', $organizational_unit));
    }
    else
    {
      $this->getLogger()
        ->log(print_r($form->getValues(), true));
      $parent_id = $form->getValue('parent_id');
      $parent_organizational_unit = OrganizationalUnitTable::getInstance()->findOneById($parent_id);
      $this->parent_organizational_unit = $parent_organizational_unit;
      $this->form_errors = BaseForm::collectErrors($form);
    }
  }
}
